package com.tigerlee.stopmusic;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class StopMusicWidget extends AppWidgetProvider{
	private MyPreference mPreference;
	
	public static final String INTENT_ACTION_INCREASE_MINS = "com.tigerlee.stopmusic.increase_mins";
	public static final String INTENT_ACTION_DECREASE_MINS = "com.tigerlee.stopmusic.decrease_mins";
	
	public static final String INTENT_ACTION_TOGGLE_ALARM = "com.tigerlee.stopmusic.togglet_alarm";
	
	private static final String TAG = "StopMusicWidget";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.e(TAG, "onUpdate called");
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.album_appwidget);
		
		mPreference = new MyPreference(context);
		if(mPreference.isRunning()){
			views.setImageViewResource(R.id.control_onoff, R.drawable.btn_widget_on);
		}else{
			views.setImageViewResource(R.id.control_onoff, R.drawable.btn_widget_off);
		}
		int alarmTime = mPreference.getWidgetTime();
		
		// Get the layout for the App Widget and attach an on-click listener to the button
		linkAlarmButtons(context, views);
		
		linkMediaButtons(context, views);
		
		// Tell the AppWidgetManager to perform an update on the current app widget
		final int N = appWidgetIds.length;
		for(int i = 0; i < N; i++){
			String alarmText = context.getString(R.string.text_pre_min_later) + alarmTime
					+ context.getString(R.string.text_post_min_later);
			views.setTextViewText(R.id.text_alarm_min_widget, alarmText);
			Log.e(TAG, alarmText);
			int appWidgetId = appWidgetIds[i];
			appWidgetManager.updateAppWidget(appWidgetId, views);
			
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	protected void linkAlarmButtons(Context context, RemoteViews views) {
		Intent intent = new Intent(context, WidgetControlReceiver.class);
		intent.setAction(INTENT_ACTION_INCREASE_MINS);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		views.setOnClickPendingIntent(R.id.control_uptime, pendingIntent);
		
		intent = new Intent(context, WidgetControlReceiver.class);
		intent.setAction(INTENT_ACTION_DECREASE_MINS);
		pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		views.setOnClickPendingIntent(R.id.control_downtime, pendingIntent);
		
		intent = new Intent(context, WidgetControlReceiver.class);
		intent.setAction(INTENT_ACTION_TOGGLE_ALARM);
		pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		views.setOnClickPendingIntent(R.id.control_onoff, pendingIntent);
	}
	
	protected void linkMediaButtons(Context context, RemoteViews views) {
		final ComponentName serviceName = new ComponentName(context, MediaPlaybackReceiver.class);
		Intent intent = new Intent(MediaPlaybackReceiver.TOGGLEPAUSE_ACTION);
		intent.setComponent(serviceName);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				0 /* no requestCode */, intent, 0 /* no flags */);
		views.setOnClickPendingIntent(R.id.control_togglepause, pendingIntent);
		intent = new Intent(MediaPlaybackReceiver.NEXT_ACTION);
		intent.setComponent(serviceName);
		pendingIntent = PendingIntent.getBroadcast(context,
				0 /* no requestCode */, intent, 0 /* no flags */);
		views.setOnClickPendingIntent(R.id.control_next, pendingIntent);

		intent = new Intent(MediaPlaybackReceiver.PREVIOUS_ACTION);
		intent.setComponent(serviceName);
		pendingIntent = PendingIntent.getBroadcast(context,
				0 /* no requestCode */, intent, 0 /* no flags */);
		views.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
	}
	
}